<?php
require_once('Kbs/Model/Business/Abstract.php');

class Kbs_Model_Business_Locale extends Kbs_Model_Business_Abstract
{
    /**
     * Mapper class name
     */
    protected $_mapperClassName = 'Kbs_Model_Mapper_Locale';

    /**
     * Protected attributes
     */
    protected $_localeId;
    protected $_localeName;
    protected $_localeTitle;
    protected $_localeDefault;

    /**
     * Get id
     */
    public function getId()
    {
        return $this->getLocaleId();
    }

    public function setId($id)
    {
        return $this->setLocaleId($id);
    }

    /**
     * Get locale id
     */
    public function getLocaleId()
    {
        return (int)$this->_localeId;
    }

    public function setLocaleId($id)
    {
        $this->_localeId = (int)$id;
        return $this;
    }

    /**
     * Get locale name
     */
    public function getLocaleName()
    {
        return (string)$this->_localeName;
    }

    public function setLocaleName($string)
    {
        $this->_localeName = (string)$string;
        return $this;
    }

    /**
     * Get locale title
     */
    public function getLocaleTitle()
    {
        return (string)$this->_localeTitle;
    }

    public function setLocaleTitle($string)
    {
        $this->_localeTitle = (string)$string;
        return $this;
    }

    /**
     * Get locale default setting
     */
    public function getLocaleDefault()
    {
        return (bool)$this->_localeDefault ? 1 : 0;
    }

    public function setLocaleDefault($isDefault)
    {
        if ((bool)$isDefault == true) {
            $this->_localeDefault = 1;
        } else {
            $this->_localeDefault = 0;
        }
        return $this;
    }



    /**
     * Init the locale for application
     */
    public function initLocale()
    {
        $session = new Zend_Session_Namespace('locale');

        // localeId = 1, 2 ...
        if (isset($session->localeId)) {

            // first we check the session
            $locale = $this->find($session->localeId);
            if ($locale) {
                $locale = new Zend_Locale($locale->localeName);
            } else {
                $locale = new Zend_Locale('auto');
            }

        } else {

            // second we check the database
            $localeObjects = $this->fetchAll();
            $locale = new Zend_Locale('auto');

            if (!empty($localeObjects)) {
                $noLocaleSpesific = true;
                foreach ($localeObjects as $localeObj) {
                    if ($localeObj->getLocaleName() == $locale->toString()) {
                        $locale = new Zend_Locale($localeObj->getLocaleName());
                        $session->localeId = $localeObj->localeId;
                        $noLocaleSpesific = false;
                    }
                    if ($localeObj->getLocaleDefault()) {
                        $default = $localeObj->getLocaleName();
                        $defaultId = $localeObj->localeId;
                    }
                }

                // third we use the default one
                if ($noLocaleSpesific) {
                    $session->localeId = $defaultId;
                    $locale = new Zend_Locale($default);
                }
            }
        }
        //var_dump($locale);die;
        return $locale;
    }

}